﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class EmailSetting : BasePage
    {
        protected Button btnGoBack;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkIsHTML;
        protected CheckBox chkISRegularSend;
        protected HtmlForm F1881;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPassword;
        protected HtmlHead Head1;
        protected Label lblAddressee;
        protected Label lblCarbonCopy;
        protected Label lblContent;
        protected Label lblDescription;
        protected Label lblEmailAccount;
        protected Label lblEmailSetting;
        protected Label lblEmailSettingID;
        protected Label lblEmalPwd;
        protected Label lblMsg;
        protected Label lblRemark;
        protected Label lblSMTP;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAddressee;
        protected TextBox txtCarbonCopy;
        protected TextBox txtContent;
        protected TextBox txtDescription;
        protected TextBox txtEmailAccount;
        protected TextBox txtEmailSettingID;
        protected TextBox txtEmalPwd;
        protected TextBox txtRemark;
        protected TextBox txtSmtp;
        protected UpdatePanel up1;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("EmailSettingQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("EmailSettingList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (base.DataEntityViewState != null)
            {
                this.UpdateMailInfo();
            }
            else
            {
                this.InsertMailInfo();
            }
        }

        private EmailSettingInfo GetMailForm(EmailSettingInfo emailSettingInfo)
        {
            emailSettingInfo.EmailSettingID = this.txtEmailSettingID.Text;
            emailSettingInfo.Description = this.txtDescription.Text;
            if (!string.IsNullOrEmpty(this.txtEmailAccount.Text.Trim()))
            {
                emailSettingInfo.EmailAccount = this.txtEmailAccount.Text;
            }
            else
            {
                emailSettingInfo.EmailAccount = base.GetValueFromAppSettings("EMAIL_ACCOUNT");
            }
            if (!string.IsNullOrEmpty(this.txtEmalPwd.Text.Trim()))
            {
                emailSettingInfo.EmalPwd = this.txtEmalPwd.Text;
            }
            else
            {
                emailSettingInfo.EmalPwd = base.GetValueFromAppSettings("EMAL_PWD");
            }
            emailSettingInfo.Addressee = this.txtAddressee.Text;
            emailSettingInfo.CarbonCopy = this.txtCarbonCopy.Text;
            emailSettingInfo.Content = this.txtContent.Text;
            emailSettingInfo.Remark = this.txtRemark.Text;
            if (!string.IsNullOrEmpty(this.txtSmtp.Text.Trim()))
            {
                emailSettingInfo.Smtp = this.txtSmtp.Text;
            }
            else
            {
                emailSettingInfo.Smtp = base.GetValueFromAppSettings("SMTP");
            }
            emailSettingInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            emailSettingInfo.IsHtml = this.chkIsHTML.Checked ? "Y" : "N";
            emailSettingInfo.IsRegularSend = this.chkISRegularSend.Checked ? "Y" : "N";
            emailSettingInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            return emailSettingInfo;
        }

        private void InsertMailInfo()
        {
            try
            {
                EmailSettingInfo emailSettingInfoInfo = new EmailSettingInfo();
                emailSettingInfoInfo = this.GetMailForm(emailSettingInfoInfo);
                emailSettingInfoInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(emailSettingInfoInfo, "EMAIL_SETTING") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = emailSettingInfoInfo;
                this.txtEmailSettingID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetOwnerInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetOwnerInfo2Form()
        {
            try
            {
                string emailSettingInfoID = base.Request.QueryString["ID"].ToString();
                EmailSettingInfo emailSettingInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(emailSettingInfoID, "EMAIL_SETTING") as EmailSettingInfo;
                if (emailSettingInfo != null)
                {
                    base.DataEntityViewState = emailSettingInfo;
                    this.txtEmailSettingID.Text = emailSettingInfo.EmailSettingID;
                    this.txtDescription.Text = emailSettingInfo.Description;
                    this.txtEmailAccount.Text = emailSettingInfo.EmailAccount;
                    this.hdnPassword.Value = emailSettingInfo.EmalPwd;
                    string strPass = "";
                    for (int i = 0; this.hdnPassword.Value.Length > i; i++)
                    {
                        strPass = strPass + "☀";
                    }
                    this.txtEmalPwd.Attributes.Add("value", strPass);
                    this.txtAddressee.Text = emailSettingInfo.Addressee;
                    this.txtCarbonCopy.Text = emailSettingInfo.CarbonCopy;
                    this.txtContent.Text = emailSettingInfo.Content;
                    this.txtRemark.Text = emailSettingInfo.Remark;
                    this.txtSmtp.Text = emailSettingInfo.Smtp;
                    this.chkIsActive.Checked = emailSettingInfo.IsActive == "Y";
                    this.chkIsHTML.Checked = emailSettingInfo.IsHtml == "Y";
                    this.chkISRegularSend.Checked = emailSettingInfo.IsRegularSend == "Y";
                    this.chkIsActive.Checked = emailSettingInfo.IsActive == "Y";
                    this.txtEmailSettingID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateMailInfo()
        {
            try
            {
                EmailSettingInfo emailSettingInfoInfo = base.DataEntityViewState as EmailSettingInfo;
                emailSettingInfoInfo = this.GetMailForm(emailSettingInfoInfo);
                string strPass = string.Empty;
                for (int i = 0; this.hdnPassword.Value.Length > i; i++)
                {
                    strPass = strPass + "☀";
                }
                if (!(string.IsNullOrEmpty(this.txtEmalPwd.Text.Trim()) || !(this.txtEmalPwd.Text != strPass)))
                {
                    emailSettingInfoInfo.EmalPwd = this.txtEmalPwd.Text;
                    this.hdnPassword.Value = this.txtEmalPwd.Text;
                }
                else
                {
                    emailSettingInfoInfo.EmalPwd = this.hdnPassword.Value;
                }
                emailSettingInfoInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(emailSettingInfoInfo, "EMAIL_SETTING") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

